package cn.com.jslh.boarpro.main.view.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.jslh.boarpro.R;
import cn.com.jslh.boarpro.base.BaseFragment;
import cn.com.jslh.boarpro.main.MainDetilActivity;
import cn.com.jslh.boarpro.main.presenter.DeliveryRecordPresenterImpl;
import cn.com.jslh.boarpro.main.presenter.SearchPresenterImpl;
import cn.com.jslh.boarpro.main.view.view.BreedingRecordView;
import cn.com.jslh.boarpro.util.FragmentFlashFlag;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;

/**
 * 分娩记录
 * 作者：周洲 on 2017/7/26 10:42
 * <p>
 * 邮箱：double0zhou@126.com
 */

public class DeliveryRecordFragment extends BaseFragment implements BreedingRecordView {
    @BindView(R.id.recycleview)
    RecyclerView recycleview;
    @BindView(R.id.dataRecycleview)
    RecyclerView dataRecycleview;
    private DeliveryRecordPresenterImpl deliveryRecordPresenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.delivery_record_layout, container, false);
        unbinder = ButterKnife.bind(this, view);
        deliveryRecordPresenter = new DeliveryRecordPresenterImpl(getContext(), this);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 3, GridLayoutManager.VERTICAL, false);
        gridLayoutManager.setAutoMeasureEnabled(true);
        recycleview.setLayoutManager(gridLayoutManager);
        deliveryRecordPresenter.setUI(recycleview);
        deliveryRecordPresenter.upDatas();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false);
        dataRecycleview.setLayoutManager(linearLayoutManager);
        dataRecycleview.addItemDecoration(new DividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL));
        deliveryRecordPresenter.setDataUI(dataRecycleview);
        deliveryRecordPresenter.upDataUI();
        return view;
    }

    @Override
    public void onItemClicked(String... params) {

    }

    // This method will be called when a SomeOtherEvent is posted
    @Subscribe
    public void handleSomethingElse(FragmentFlashFlag flag) {
        deliveryRecordPresenter.upDataUI();
    }

    public static void FlashUI(){
        EventBus.getDefault().post(new FragmentFlashFlag());
    }

    @Subscribe
    public void handleUpDataAction(MainDetilActivity.Action action){
        if(isResumed()&&((MainDetilActivity)getContext()).getViewPage().getCurrentItem()==0) {
            deliveryRecordPresenter.upActionDatas();
        }
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}
